/**
 * Created by yangfan on 2016/12/28.
 */
/**
 * Created by yangfan on 2016/11/9.
 */
define(["require",'config/appregister',"i18n","business/news/services/httpServices", "business/services/rightComponents"],function(require,app,i18n,httpServices){
    app.controller('searchNewsCtrl',newsListCtrl)
    function newsListCtrl($scope,$state ,$rootScope){

        $scope.isShowLabel = [];
        $scope.isHoverStyle = [];
        $scope.newListEnter = function(index){
            $scope.isHoverStyle = [];
            $scope.isHoverStyle[index] = {
                background:"#F9F8F7"
            };
        };
        $scope.newListLeave = function(){
            $scope.isHoverStyle = [];
        };

        $scope.listParams = {
            typeId:0,
            keyWord:"",
            time:true,
            pageNo:1,
            pageSize:10
        };
        if(sessionStorage.hasOwnProperty("searchKeyWord")){
            $scope.listParams.keyWord = sessionStorage.searchKeyWord ;
        };
        function getList(){
            var req = {
                type:"GET",
                uri:"getList",
                data:{
                    typeId : $scope.listParams.typeId,
                    keyWord:$scope.listParams.keyWord,
                    time:$scope.listParams.time,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize
                }
            };
            httpServices.newsHttpServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.list = response.data.data.list;
                    $scope.totalItem = response.data.data.Pagination.totalCount;
                    getListModel()
                })
            }).error(function(){

            })


        }

        function getTypeList(){
            var req = {
                type:"GET",
                uri:"getSecondaryCategory",
                data:{
                    tcId:8
                }
            };
            httpServices.getNewsCategory(req).success(function(response){
                $scope.$apply(function(){
                    $scope.newsListLabel = response.data;
                })
            }).error(function(){

            })

        }

        function getHotRank(){
            var req = {
                type:"GET",
                uri:"getHotRank",
                data:{
                    count:10
                }
            };
            httpServices.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.getHotRank = response.data;
                    getDownLoadRankConf($scope.getHotRank);
                })
            });
        };
        function getKeyWord(){
            var req = {
                type:"GET",
                uri:"getKeyWord",
                data:{
                    count:10
                }
            };
            httpServices.newsHttpServices(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.relatedSearch = response.data;
                })
            });
        };

        function getDownLoadRankConf(data){
            $scope.setConf = {
                data:data,
                idType:"newsId",
                state:"eng.newsDetail",
                title:'头条排行',
                icon:true
            }
        }
        getList();
        getTypeList();
        getHotRank();
        getKeyWord();
        $scope.isShowLabel[0] = {
            border:"1px solid #00A4FE"
        };
        $scope.clickTagLabel = function(scId){
            $scope.listParams.typeId = scId;
            $scope.listParams.pageNo = 1;
            getList();
        };
        $scope.goTo = function(item){
            $state.go("eng.newsDetail",{newsId:item.newsId});
        };

        function getListModel(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                itemsPerPage:$scope.listParams.pageSize,
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getList();
                }
            };
        };

        //相关资讯分类跳转
        $scope.jumpToSearch = function(item){
            $rootScope.GLOBALKEYWORD =  item.name;
            sessionStorage.searchKeyWord = item.name;
            $state.transitionTo("search.news", {}, {
                reload: true, inherit: false, notify: true
            });
        }




    }
});

